package com.hugh.commom.delayqueue;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @Author: Hugh
 * @Date: 2020/6/25
 */
public class DelayTask implements Delayed {

    final private Object data;
    final private long expire;

    public DelayTask(Object data, long expire) {
        this.data = data;
        this.expire = expire + System.currentTimeMillis();
    }

    public Object getData() {
        return data;
    }

    public long getExpire() {
        return expire;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(this.expire - System.currentTimeMillis(), unit);
    }

    @Override
    public int compareTo(Delayed o) {
        long delta = getDelay(TimeUnit.NANOSECONDS) - o.getDelay(TimeUnit.NANOSECONDS);
        return (int) delta;
    }
}
